<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\agent\controller;

class AdminAuthAppController extends \app\AgentRest
{
    private $auth_app_name = "";
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        if(isset($this->_param["app_name"])) {
            $this->auth_app_name = $this->_param["app_name"];
        }
        $this->error(lang("app_name is empty"));
        if($this->_user["role_name"] != "admin") {
            echo json_encode(["code" => 401, "error" => lang("Permission denied")]);
            exit;
        }
    }
    public function list()
    {
        $param = $this->_param;
        if(!isset($param["app_name"])) {
            return $this->error("参数错误");
        }
        if($this->_is_weiqin) {
            $app_model_name = APP_MODEL_NAME;
            $list = \app\agent\model\Cardauth2AuthAppModel::alias("a")->field(["a.id", "a.modular_id", "a. create_time", "a.sign", "c.mini_app_name"])->join("longbing_card_config c", "a.modular_id = c.uniacid")->join("account", "a.modular_id = account.uniacid")->join("wxapp_versions v", "a.modular_id = v.uniacid")->where([["a.status", "=", 1], ["app_name", "like", "%" . $param["app_name"] . "%"], ["account.type", "=", 4], ["account.isdeleted", "=", 0], ["v.modules", "like", "%" . $app_model_name . "%"]])->group("a.id")->paginate(["list_rows" => $param["page_count"] ? $param["page_count"] : 10, "page" => $param["page"] ? $param["page"] : 1])->toArray();
        } else {
            $list = \app\agent\model\Cardauth2AuthAppModel::alias("a")->field(["a.id", "a.modular_id", "a. create_time", "a.sign", "c.mini_app_name"])->join("longbing_card_config c", "a.modular_id = c.uniacid")->where([["a.status", "=", 1], ["app_name", "=", $param["app_name"]]])->group("a.id")->paginate(["list_rows" => $param["page_count"] ? $param["page_count"] : 10, "page" => $param["page"] ? $param["page"] : 1])->toArray();
        }
        $wxapp_map = [];
        $wxapp = \think\facade\Db::name("account_wxapp")->field(["uniacid", "name"])->select();
        foreach ($wxapp as $item) {
            $wxapp_map[$item["uniacid"]] = $item["name"];
        }
        foreach ($list["data"] as $k => $item) {
            $list["data"][$k]["name"] = $wxapp_map[$item["modular_id"]] ?? $item["mini_app_name"];
            unset($list["data"][$k]["mini_app_name"]);
        }
        $list["total_number"] = \app\agent\service\AdminUserService::getSassNum($param["app_name"], $this->_uniacid);
        $list["total_used"] = \app\agent\model\Cardauth2AuthAppModel::where([["uniacid", "in", $this->_uniacid_arr], ["app_name", "=", $param["app_name"]]])->sum("count");
        return $this->success($list);
    }
    public function create()
    {
        $data = $this->_input;
        if(!isset($data["modular_id"]) || !isset($data["app_name"])) {
            return $this->error("参数错误");
        }
        $num = \app\agent\service\AdminUserService::getSassNum($data["app_name"], $this->_uniacid);
        $time = time();
        $auth = \app\agent\model\Cardauth2AuthAppModel::where([["modular_id", "=", $data["modular_id"]], ["app_name", "=", $data["app_name"]]])->findOrEmpty();
        if(!$auth->isEmpty()) {
            return $this->error("已存在此小程序");
        }
        $total_num = \app\agent\model\Cardauth2AuthAppModel::where([["uniacid", "in", $this->_uniacid_arr], ["app_name", "=", $data["app_name"]]])->sum("count");
        $remain = $num - $total_num;
        if($remain <= 0) {
            return $this->error("分配的数量超过可用的总数");
        }
        $rst = $auth->save(["modular_id" => $data["modular_id"], "create_time" => $time, "update_time" => $time, "sign" => intval($time + 31622400), "count" => 1, "uniacid" => $this->_uniacid, "app_name" => $data["app_name"], "sign_data" => "ndvjnfjvnjnv" . $time]);
        if($rst) {
            return $this->success("success");
        }
        return $this->error("fail");
    }
    public function extendedOneYear()
    {
        $data = $this->_input;
        if(!isset($data["modular_id"]) || !isset($data["app_name"])) {
            return $this->error("参数错误");
        }
        $time = time();
        $auth = \app\agent\model\Cardauth2AuthAppModel::where([["modular_id", "=", $data["modular_id"]], ["app_name", "=", $data["app_name"]]])->findOrEmpty();
        if($auth->isEmpty()) {
            return $this->error("小程序不存在");
        }
        $num = \app\agent\service\AdminUserService::getSassNum($data["app_name"], $this->_uniacid);
        $total_used = \app\agent\model\Cardauth2AuthAppModel::where([["uniacid", "in", $this->_uniacid_arr], ["app_name", "=", $data["app_name"]]])->sum("count");
        $remain = $num - $total_used;
        if($remain <= 0) {
            return $this->error("分配的数量超过可用的总数");
        }
        $rst = $auth->save(["sign" => $time < $auth["sign"] ? $auth["sign"] + 31622400 : $time + 31622400, "count" => $auth["count"] + 1, "update_time" => $time]);
        if($rst) {
            return $this->success("success");
        }
        return $this->error("fail");
    }
}

?>